package cn.dankal.lequ.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.dankal.lequ.R;
import cn.dankal.lequ.commom.adapter.FragmentAdapter;
import cn.dankal.lequ.commom.util.Constant;
import cn.dankal.lequ.ui.activity.DownloadActivity;
import cn.dankal.lequ.ui.activity.GameDetailsActivity;
import cn.dankal.lequ.ui.activity.HistoryActivity;
import cn.dankal.lequ.ui.customview.MyConvenientBanner;
import cn.dankal.lequ.ui.fragment.comprehensive_fragment.AlbumFragment;
import cn.dankal.lequ.ui.fragment.comprehensive_fragment.ClassifyFragment;
import cn.dankal.lequ.ui.fragment.comprehensive_fragment.CompreFragment;
import cn.dankal.lequ.ui.fragment.comprehensive_fragment.RankingFragment;
import cn.dankal.lequ.ui.fragment.comprehensive_fragment.YoulikeFragment;

/**
 * Created by 苏彬 on 2016/4/14.
 */
public class ComprehensiveFragment extends Fragment {
    @Bind(R.id.convenientBanner)
    MyConvenientBanner convenientBanner;
    @Bind(R.id.view_pager)
    ViewPager view_pager;
    @Bind(R.id.tab_layout)
    TabLayout tab_layout;
    @Bind(R.id.tv_search)
    TextView tv_search;
    List<Integer> localImages = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_independentagameandcompre, container, false);
        ButterKnife.bind(this, view);
        return view;
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        showAdvertisement();
        initTabLayout();
        Drawable drawable1 = getResources().getDrawable(R.mipmap.homepage_search);
        drawable1.setBounds(0, 0, 35, 35);//第一0是距左边距离，第二0是距上边距离，40分别是长宽
        tv_search.setCompoundDrawables(null, null, drawable1, null);//只放左边

    }


    private void initTabLayout() {
        List<android.support.v4.app.Fragment> fragments = new ArrayList<>();
        ArrayList<String> tablayout_titles = new ArrayList<>();
        tablayout_titles.add(Constant.COMPREHENSIVE);
        fragments.add(new CompreFragment());
        tablayout_titles.add(Constant.CLASSIFY);
        fragments.add(new ClassifyFragment());
        tablayout_titles.add(Constant.RANKING);
        fragments.add(new RankingFragment());
        tablayout_titles.add(Constant.ALBUM);
        fragments.add(new AlbumFragment());
        tablayout_titles.add(Constant.YOULIKE);
        fragments.add(new YoulikeFragment());
        for (int i = 0; i < tablayout_titles.size(); i++) {
            tab_layout.addTab(tab_layout.newTab().setText(tablayout_titles.get(i)));
        }
        FragmentAdapter adapter = new FragmentAdapter(getFragmentManager(), fragments, tablayout_titles);
        view_pager.setAdapter(adapter);
        tab_layout.setupWithViewPager(view_pager);
        tab_layout.setTabsFromPagerAdapter(adapter);
    }


    /*
    *
    * 轮播广告
    * */
    private void showAdvertisement() {
        localImages.add(R.mipmap.zhanzheng);
        localImages.add(R.mipmap.chongtu);
        localImages.add(R.mipmap.haizu);
        localImages.add(R.mipmap.huoying);
        localImages.add(R.mipmap.biaoche);
        convenientBanner.setPages(new CBViewHolderCreator<LocalImageHolderView>() {
            @Override
            public LocalImageHolderView createHolder() {
                return new LocalImageHolderView();
            }
        }, localImages)
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
                .setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        Intent intent = new Intent(ComprehensiveFragment.this.getActivity(), GameDetailsActivity.class);
                        startActivity(intent);
                    }
                });
    }

    // 开始自动翻页
    @Override
    public void onResume() {
        super.onResume();
        //开始自动翻页
        convenientBanner.startTurning(5000);
    }

    // 停止自动翻页
    @Override
    public void onPause() {
        super.onPause();
        //停止翻页
        convenientBanner.stopTurning();
    }

    @OnClick({R.id.download, R.id.message, R.id.tv_search})
    public void Click(View v) {
        switch (v.getId()) {
            case R.id.download:
                startActivity(new Intent(getActivity(), DownloadActivity.class));
                break;
            case R.id.message:
                break;
            case R.id.tv_search:
                startActivity(new Intent(getActivity(), HistoryActivity.class));
                break;
            default:
                break;

        }
    }

    public class LocalImageHolderView implements Holder<Integer> {
        private ImageView imageView;

        @Override
        public View createView(Context context) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageView;
        }
        @Override
        public void UpdateUI(Context context, final int position, Integer data) {
            imageView.setImageResource(data);
        }
    }

}
